package com.pat.jrgwt.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;


public class FxWeighting extends BaseMonthTableView
{
    protected FlexTable m_table = new FlexTable();
    

    private static class SingletonHolder
    {
        private static final FxWeighting instance = new FxWeighting();
    }
    
    public static FxWeighting getInstance()
    {
        return SingletonHolder.instance;
    }
    
    private FxWeighting()
    {
        setLabel("FX-Weighting");
        m_arCellValues = new double[m_arRowLabels.length][m_arHeaders.length];
        m_valueFormatter = NumberFormat.getFormat("#");
//        VerticalPanel p = new VerticalPanel();
//        
//        Label lbl = new Label("FX-Weighting");
//        lbl.setStylePrimaryName("cellBold");
//        p.add(lbl);
//        
//        formatTable();
//        clearValues();
//        p.add(m_table);
//        
//        initWidget(p);
    }

    public void recalculate()
    {
//        NumberFormat nf = NumberFormat.getFormat("#");
        ProfitLossesHolder pfHolder = ValuesStorage.getInstance().getProfitLosses();
////        String[] arExchanges = pfHolder.getExchangeNames();
        String[] arExchanges = new String[] { "EURUSD-30", "EURNZD-120", "USDJPY-60", "USDJPY-120", "AUDJPY-120", "GBPJPY-60" };
        double v = 0;
        double sum = 0;
        int column = 1;
        for (int month = 1; month < 13; month++)
        {
            column = 1;
            // EUR calculation
            sum = 0;
            v = pfHolder.get(arExchanges[0], month) == 0 ? 0 : 1;
            sum += v;
//            sum += v == 0 ? 0 : 1;
//            m_table.setText(month, column++, v == 0 ? "0" : "1");
            setAt(month, column, v);
            column++;
            v = pfHolder.get(arExchanges[1], month) == 0 ? 0 : 1;
            sum += v;
            setAt(month, column, v);
            column++;
            
            setAt(month, column, sum);
            column++;
//            m_table.setText(month, column++, v == 0 ? "0" : "1");
//            m_table.setText(month, column, nf.format(sum));
//            m_table.getCellFormatter().setStylePrimaryName(month, column++, "cellBold");
            
            // USD calculation
            sum = 0;
            v = pfHolder.get(arExchanges[2], month) == 0 ? 0 : 1;
            sum += v;
            setAt(month, column, v);
            column++;
//            m_table.setText(month, column++, v == 0 ? "0" : "1");
            v = pfHolder.get(arExchanges[3], month) == 0 ? 0 : 1;
            sum += v;
            setAt(month, column, v);
            column++;
            
            setAt(month, column, sum);
            column++;
//            m_table.setText(month, column++, v == 0 ? "0" : "1");
//            m_table.setText(month, column, nf.format(sum));
//            m_table.getCellFormatter().setStylePrimaryName(month, column++, "cellBold");
            
            // USD calculation
            sum = 0;
            v = pfHolder.get(arExchanges[4], month) == 0 ? 0 : 1;
            sum += v;
            setAt(month, column, v);
            column++;
//            m_table.setText(month, column++, v == 0 ? "0" : "1");
            v = pfHolder.get(arExchanges[5], month) == 0 ? 0 : 1;
            sum += v;
            setAt(month, column, v);
            column++;
//            m_table.setText(month, column++, v == 0 ? "0" : "1");
        }
        
//        setAt(1, 1, 11);
//        setAt(1, 3, 13);
//        setAt(4, 1, 41);
        
        drawTable();
    }
    
    public void clearValues()
    {
        int columns = m_table.getCellCount(0);
        for (int column = 1; column < columns; column++)
        {
            for (int row = 1; row < 13; row++)
            {
                m_table.setText(row, column, "0");
            }
        }
    }

    private void formatTable()
    {
        m_table.setBorderWidth(1);
        String[] arHeaders = new String[] { "EUR1", "EUR2", "Sum(EUR)", "USD1", "USD2", "SUM(USD)", "AUD", "GBP" };
        
        for (int column = 1; column <= arHeaders.length; column++)
        {
            m_table.setText(0, column, arHeaders[column - 1]);
            m_table.getCellFormatter().setStylePrimaryName(0, column, "cellHeader");
        }
        
        for (int month = 1; month < 13; month++)
        {
            m_table.setText(month, 0, "" + month);
        }
        
        final YearSelector ys = new YearSelector();
        ys.addYearChangeHandler(new ChangeHandler()
        {
            
            @Override
            public void onChange(ChangeEvent event)
            {
                Window.alert("Year is changed to: " + ys.getSelectedYear());
            }
        });
        m_table.setWidget(0, 0, ys);
        
    }

    @Override
    public void onYearChanged(int year)
    {
        GWT.log("Year was changed to: " + year);
    }

    @Override
    protected void setHeaders()
    {
        m_arHeaders = new String[] { "EUR1", "EUR2", "Sum(EUR)", "USD1", "USD2", "SUM(USD)", "AUD", "GBP" };
    }

}
